/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ShowPictureDialog.java
 *

 */

package jmenu.gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.border.Border;
import javax.tools.JavaFileManager.Location;
import jmenu.dbmodels.MGetImage;
import jmenu.dbmodels.MWeightsOfUser;
import jmenu.main.Config;
import jmenu.main.Icons;

/**
 *
 * @author Roman
 */
public class ShowWeightGraphDialog extends javax.swing.JDialog {

    /** Creates new form ShowPictureDialog */
    public ShowWeightGraphDialog(java.awt.Frame parent, boolean modal)
    {
        super(parent, modal);
        initComponents();
        set();
    }

    private void set()
    {
        String data = "";
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;

        BufferedImage imgUrl = null;

        MWeightsOfUser weights = Config.getKernel().queries.loadAllWeightOfUser(Config.getProfile().getId());

        for (int i = 0; i < weights.getRows().size(); i++)
        {
            if ( min > weights.getRow(i).getWeight() )
                min = weights.getRow(i).getWeight();

            if ( max < weights.getRow(i).getWeight() )
                max = weights.getRow(i).getWeight();
        }
        for (int i = 0; i < weights.getRows().size(); i++)
        {
            if (i == 0)
                data = data.concat(String.valueOf(weights.getRow(i).getWeight()));
            else
                data = data.concat(','+ String.valueOf(weights.getRow(i).getWeight()));
        }


        try
        {
            URL url = new URL("http://chart.apis.google.com/chart?cht=lc&chco=76A4FB&chds="+min+","+max+"&chs=400x250&chxt=y&chxr=0,"+min+","+max+"&chd=t:"+data);
            imgUrl = ImageIO.read(url);

            ImageIcon imgr = new ImageIcon(imgUrl);
            this.pictureLabel.setIcon(imgr);
            this.pictureLabel.setSize(imgr.getIconWidth(), imgr.getIconHeight());
            this.pictureLabel.setToolTipText("Zavřít");
            this.pictureLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
            this.setSize(imgr.getIconWidth() + 18, imgr.getIconHeight() + 43);
            this.setResizable(true);
            this.setLocationRelativeTo(getOwner());
            this.setVisible(true);

        } catch (IOException e)
        {
            this.dispose();
            JOptionPane.showMessageDialog(Config.getMainFrame(), " Nelze vytvořit graf!");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pictureLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Historie váhy uživatele");
        setBackground(java.awt.Color.black);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(new java.awt.Color(255, 255, 255));

        pictureLabel.setBackground(new java.awt.Color(255, 255, 255));
        pictureLabel.setForeground(new java.awt.Color(255, 255, 255));
        pictureLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        pictureLabel.setAlignmentY(0.0F);
        pictureLabel.setName("pictureLabel"); // NOI18N
        pictureLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pictureLabelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pictureLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pictureLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    private void pictureLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pictureLabelMouseClicked
    this.dispose();
}//GEN-LAST:event_pictureLabelMouseClicked



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel pictureLabel;
    // End of variables declaration//GEN-END:variables

}
